<?php
/**
 * @特惠赚 角色控制器
 * Author: 小谢
 * Date: 2019-5-8
 */
namespace app\admin\controller; 
use think\Controller;
use think\Db;
use think\facade\Session;
use think\facade\View;

class Role extends Base {

    //角色列表
    public function index(){
        $list = M('admin_role')->order('role_id desc')->select();
        if(is_array($list)) {
            foreach ($list as $key => $v) {
                 $row = M('admin')->where(array('role_id'=>$v['role_id']))->find();
                 $data[] = $v;
                 $data[$key]['admin_name'] = $row['user_name'] ?  $row['user_name'] : '--';
            }
        }
        View::share(["list"=>$data,'count'=>count($data)]);
        return view();
    }
    
    //角色信息
    public function role_info(){
        $role_id = I('get.role_id/d');
        $detail = array();
        if($role_id){
            $detail = M('admin_role')->where("role_id",$role_id)->find();
            $detail['act_list'] = explode(',', $detail['act_list']);
        }
        
        $right = M('system_menu')->order('id')->select();
        if($right) {
            foreach ($right as $val){
                if(!empty($detail)){
                    $val['enable'] = in_array($val['id'], $detail['act_list']);
                }
                $modules[$val['group']][] = $val;
            }
        }
        //权限组
        $group = $this->groupList();
        $modules = isset($modules) ? $modules : array();
        $detail  = isset($detail) ? $detail : array();
        View::share(["group"=>$group,'modules'=>$modules,"detail"=>$detail]);
        return view();
    }
    
    //操作角色
    public function roleSave(){
        $data = I('post.');
        $res = $data['data'];
        $res['act_list'] = is_array($data['right']) ? implode(',', $data['right']) : '';

        if(empty($res['act_list'])) {
            $this->error("请选择权限!");        
        }

        if(empty($data['role_id'])){
            $admin_role = M('admin_role')->where(['role_name'=>$data['roleName']])->find();
            if($admin_role){
                $this->error("已存在相同的角色名称!");
            } else {
                $res['role_name'] = $data['roleName'];
                $r = M('admin_role')->add($res);
            }
        }else{
            $admin_role = M('admin_role')->where(['role_name'=>$data['roleName'],'role_id'=>['<>',$data['role_id']]])->find();
            if($admin_role){
                $this->error("已存在相同的角色名称!");
            }else{
                $res['role_name'] = $data['roleName'];
                $r = M('admin_role')->where('role_id', $data['role_id'])->save($res);
            }
        }
        
        if($r){
            adminLog('管理角色');
            $this->success("操作成功!",U('Admin/role/index'));
        }else{
            $this->error("操作失败!",U('Admin/role/index'));
        }
    }
    
    //删除角色
    public function roleDel(){
        $role_id = I('post.role_id/d');
        $admin = M('admin')->where('role_id',$role_id)->find();
        if($admin) {
            $this->ajaxReturn("请先清空所属该角色的管理员");
        } else {
            $result = M('admin_role')->where("role_id", $role_id)->delete();
            if($result){
                $this->ajaxReturn(1);
            }
            $this->ajaxReturn("删除失败");
        }
    }
      
}